<template>
  <a-modal 
    :confirm-loading="modalOption.loading" 
    :okText="modalOption.okText" 
    @ok="modalOption.onOk"
    @cancel="modalOption.onCancel" 
    :cancelText="modalOption.cancelText" 
    :width="modalOption.width" 
    :visible="visible"
    :title="modalOption.title"
  >
    <a-spin tip="loading" :spinning="loading">
        <div class="container">
        <a-form-model 
          ref="formModel" 
          :label-col="formOption.labelCol" 
          :wrapper-col="formOption.wrapperCol"
          :layout="formOption.layout" 
          :label-align="formOption.labelAlign" 
          :hide-required-mark="formOption.hideRequiredMark"
          :rules="formOption.rules" 
          :model="formOption.formData"
        >
        <!-- 生产管理室表单 -->
        <div v-if="tabName === '生产管理室'">
            <a-card title="基本信息" hoverable>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="登记日期" prop="registrationDate" :rules="[{ required: true, message: '登记日期不能为空' }]">
                            <a-date-picker :locale="locale" v-model="formOption.formData.registrationDate" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="品种类型" prop="varietyType" :rules="[{ required: true, message: '品种类型不能为空' }]">
                            <a-select :disabled="viewDetail" @change="standardChange" v-model="formOption.formData.varietyType" placeholder="请选择品种类型">
                                <a-select-option value="国标">国标</a-select-option>
                                <a-select-option value="省标">省标</a-select-option>
                                <a-select-option value="省标-北京">省标-北京</a-select-option>
                                <a-select-option value="省标-上海">省标-上海</a-select-option>
                                <a-select-option value="国标-北京">国标-北京</a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="类别" prop="type">
                            <a-select :disabled="viewDetail" @change="()=>getProductNameList(formOption.formData.varietyType)" v-model="formOption.formData.type" placeholder="请选择类别">
                                <a-select-option value="大袋">大袋</a-select-option>
                                <a-select-option value="小袋">小袋</a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="品名" prop="name" :rules="[{ required: true, message: '品名不能为空' }]">
                            <a-select show-search allow-clear :filter-option="$filterOption" v-if="formOption.formData.varietyType" :disabled="viewDetail" v-model="formOption.formData.name" placeholder="请选择品名">
                                <a-select-option v-for="(item,i) in searchSource.productNameList" :key="i" :value="item.name">{{ item.name }}</a-select-option>
                            </a-select>
                            <span v-else>请先选择品种类型</span>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="成品库存天数（登记时）" prop="packDayNumReg">
                            <a-input v-model="formOption.formData.packDayNumReg" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="未扣库存前成品库存天数" prop="packDaySubNum">
                            <a-input v-model="formOption.formData.packDaySubNum" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    
                    <a-col :span="8">
                        <a-form-model-item label="成品天数（O列）" prop="packDayNum">
                            <a-input v-model="formOption.formData.packDayNum" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="成品发货后天数（R列）" prop="packDayShipNum">
                            <a-input v-model="formOption.formData.packDayShipNum" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="最新生产状态" prop="prodState">
                            <a-input v-model="formOption.formData.prodState" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    
                    <a-col :span="8">
                        <a-form-model-item label="生产类型" prop="prodType">
                            <a-input v-model="formOption.formData.prodType" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="提取车间" prop="extractWorkshop">
                            <a-input v-model="formOption.formData.extractWorkshop" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="药材批号" prop="ycBatch">
                            <a-input v-model="formOption.formData.ycBatch" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="药材合格日期" prop="ycQualityDate">
                            <a-date-picker :locale="locale" v-model="formOption.formData.ycQualityDate" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="计划交货时间" prop="deliveryDate">
                            <a-date-picker :locale="locale" v-model="formOption.formData.deliveryDate" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>

        <!-- 饮片-提取-制剂车间表单 -->
        <div v-if="tabName === '饮片-提取-制剂车间'">
            <h2>品名：{{formOption.formData.name}}</h2>
            <a-card title="饮片车间" hoverable>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="药材领用时间" prop="ycTakeDate">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.ycTakeDate" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
            <a-card title="提取/制剂车间1" hoverable style="margin-top: 16px;">
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="提取物批号" prop="tqwBatch" :rules="[{ required: true, message: '提取物批号不能为空' }]">
                            <a-input :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.tqwBatch" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="来料批号" prop="rawYpBatch" :rules="[{ required: true, message: '来料批号不能为空' }]">
                            <div v-if="formOption.formData.tempState !=='2'">
                                <div v-for="(rb,i) in formOption.formData.rawYpBatch" :key="i" style="display:flex;gap:8px;margin-bottom:8px;">
                                    <a-input v-model="rb.batch" style="flex:1" />
                                    <a-button type="primary" @click="addRawBatchItem">新增</a-button>
                                    <a-button type="danger" @click="deleteRawBatchItem(i)" :disabled="formOption.formData.rawYpBatch.length<=1">删除</a-button>
                                </div>
                            </div>
                            <div v-else>
                                <div v-for="(rb,i) in formOption.formData.rawYpBatch" :key="i">{{rb.batch}}</div>
                            </div>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="提取生产开始时间" prop="tqStartTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.tqStartTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="过筛结束时间" prop="gsEndTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.gsEndTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="混一送样时间" prop="hySampleTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.hySampleTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="送检批号（小样）" prop="sampleBatchSmall" :rules="[{ required: true, message: '送检批号（小样）不能为空' }]">
                            <a-input :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.sampleBatchSmall" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="提取/制剂记录送审合格日期" prop="sampleDate">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.sampleDate" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
            <a-card title="提取/制剂车间2" hoverable style="margin-top: 16px;">
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="PC物料方案发放时间" prop="pcDistributeDate">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.pcDistributeDate" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="PC方案去向" prop="pcPlanDirection">
                            <a-input :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.pcPlanDirection" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
            <a-card title="提取车间" hoverable style="margin-top: 16px;">
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="提取接收方案时间" prop="tqReceivingPlanTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.tqReceivingPlanTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="粉碎完成时间" prop="crushingCompletionTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.crushingCompletionTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
            <a-card title="制剂车间1" hoverable style="margin-top: 16px;">
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="制剂接收方案时间" prop="zjReceivingPlanTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.zjReceivingPlanTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="制粒完成时间" prop="zlFinishTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.zlFinishTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="颗粒批号" prop="klBatch" :rules="[{ required: true, message: '颗粒批号不能为空' }]">
                            <a-input :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.klBatch" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="是否FS半成品" prop="fsFlag">
                            <a-select :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.fsFlag" placeholder="请选择">
                                <a-select-option value="是">是</a-select-option>
                                <a-select-option value="否">否</a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="未制粒原因" prop="notZlReason">
                            <a-input :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.notZlReason" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="制剂记录送审合格日期" prop="sampleDate2">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.sampleDate2" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
            <a-card title="制剂车间2" hoverable style="margin-top: 16px;">
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="计划包装时间" prop="planPackTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.planPackTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="包装完成时间" prop="packFinishTime">
                            <a-date-picker :disabled="formOption.formData.tempState ==='2'" :locale="locale" v-model="formOption.formData.packFinishTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="成品批号" prop="prodBatch" :rules="[{ required: true, message: '成品批号不能为空' }]">
                            <a-input :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.prodBatch" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="送检批号（N）" prop="sampleBatchN">
                            <a-input :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.sampleBatchN" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="未包装原因" prop="notPackReason">
                            <a-input :disabled="formOption.formData.tempState ==='2'" v-model="formOption.formData.notPackReason" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>
        <!-- 技术部表单 -->
        <div v-if="tabName === '技术部'">
            <a-card title="基本信息" hoverable>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="送检批号（小样）：" prop="sampleBatchSmall">
                        {{formOption.formData.sampleBatchSmall}}
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="提取物收样时间" prop="tqwSampleTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.tqwSampleTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="检验部门" prop="checkDepart">
                            <a-input v-model="formOption.formData.checkDepart" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="提取物检验结果出具时间" prop="tqwCheckResultTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.tqwCheckResultTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item label="技术部制剂方案发放时间" prop="zjPlanDistributeTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.zjPlanDistributeTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                     <a-col :span="8">
                        <a-form-model-item label="方案去向" prop="planDirection">
                            <a-input v-model="formOption.formData.planDirection" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-model-item label="是否需备案" prop="recordFlag">
                            <a-select v-model="formOption.formData.recordFlag" placeholder="请选择">
                                <a-select-option value="是">是</a-select-option>
                                <a-select-option value="否">否</a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>
        <!-- 物控部表单 -->
        <div v-if="tabName === '物控部'">
            <a-card title="基本信息" hoverable>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="颗粒批号" prop="klBatch">
                            {{formOption.formData.klBatch}}
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="成品批号" prop="prodBatch">
                            {{formOption.formData.prodBatch}}
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="颗粒送FS时间" prop="klFsTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.klFsTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="成品送FS时间" prop="prodFsTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.prodFsTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="颗粒FS回厂时间" prop="klFsBackTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.klFsBackTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="成品FS回通知送样时间" prop="prodFsNoticeTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.prodFsNoticeTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
                <a-row>
                    <a-col :span="12">
                        <a-form-model-item label="物控部成品移交成品库时间" prop="prodTurnTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.prodTurnTime" valueFormat="YYYY-MM-DD" style="width: 100%" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>
        <!-- 质管部QA表单 -->
        <div v-if="tabName === '质管部QA'">
            <a-card title="颗粒QA" hoverable>
                <a-row>
                    <a-col :span="24">
                        <div>颗粒批号：{{formOption.formData.granuleBatchNo}}</div>
                        <div style="margin-top:8px">状态：{{ computeStatus('klReleaseTime','klNotReleaseReason') }}</div>
                    </a-col>
                </a-row>
                <a-row style="margin-top:12px">
                    <a-col :span="12">
                        <a-form-model-item label="颗粒放行日期" prop="klReleaseTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.klReleaseTime" valueFormat="YYYY-MM-DD" style="width: 100%" :disabled="!!formOption.formData.klNotReleaseReason" @change="onGranuleReleaseChange" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="未放行原因" prop="klNotReleaseReason">
                            <a-textarea v-model="formOption.formData.klNotReleaseReason" :disabled="!!formOption.formData.klReleaseTime" rows="4" @input="onGranuleReasonChange" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
            <a-card title="成品QA" hoverable style="margin-top: 16px;">
                <a-row>
                    <a-col :span="24">
                        <div>成品批号：{{formOption.formData.finishedBatchNo}}</div>
                        <div style="margin-top:8px">状态：{{ computeStatus('prodReleaseTime','prodNotReleaseReason') }}</div>
                    </a-col>
                </a-row>
                <a-row style="margin-top:12px">
                    <a-col :span="12">
                        <a-form-model-item label="成品放行时间" prop="prodReleaseTime">
                            <a-date-picker :locale="locale" v-model="formOption.formData.prodReleaseTime" valueFormat="YYYY-MM-DD" style="width: 100%" :disabled="!!formOption.formData.prodNotReleaseReason" @change="onFinishedReleaseChange" />
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-model-item label="未放行原因" prop="prodNotReleaseReason">
                            <a-textarea v-model="formOption.formData.prodNotReleaseReason" :disabled="!!formOption.formData.prodReleaseTime" rows="4" @input="onFinishedReasonChange" />
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-card>
        </div>
         </a-form-model>
        </div>
  </a-spin>
  <template slot="footer">
    <div style="text-align:center">
      <a-button v-if="tabName==='饮片-提取-制剂车间'" @click="handleDraft" style="margin-right: 8px">暂存</a-button>
      <a-button type="primary" @click="handleSubmit" style="margin-right: 8px">保存</a-button>
      <a-button @click="handleCancel">取消</a-button>
    </div>
  </template>
  </a-modal>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'
import nextApi from '@/api/nextApi'
import locale from "ant-design-vue/lib/date-picker/locale/zh_CN";
@Component({
  name: 'AddOrEdit'
})
export default class AddOrEdit extends Vue {
    @Prop({ type: Boolean, default: false }) visible!: boolean
    @Prop({ type: Boolean, default: false }) viewDetail!: boolean
    @Prop({ type: Object, default: () => ({}) }) editData!: any
    @Prop({type:String, default:""}) tabName!:string

    locale = locale
    loading = false

// 弹窗配置
modalOption = {
    loading: false,
    okText: '保存',
    cancelText: '取消',
    width: "90%",
    title: '',
    onOk: this.handleSubmit,
    onCancel: this.handleCancel
}

    // 表单配置
formOption:any = {
    labelCol: { span: 8 },
    wrapperCol: { span: 15},
    layout: 'horizontal',
    labelAlign: 'right',
    hideRequiredMark: false,
    formData: {},
}

// 搜索数据源
searchSource:any = {
    productNameList: [],
}


@Watch('visible')
onVisibleChange(val: boolean) {
    if (val) {
        this.$nextTick(()=>{
            this.initForm()
        })
    }
}

     // 初始化表单
initForm() {
    (this.$refs.formModel as any).resetFields()
    if(this.editData.id) {
        this.modalOption.title = this.tabName+'-编辑'
        this.formOption.formData = this.editData
        if(this.tabName==='生产管理室' && this.formOption.formData.varietyType){
            this.getProductNameList(this.formOption.formData.varietyType)
        }
        if(this.tabName==='饮片-提取-制剂车间'){
            // console.log(this.formOption.formData);
            this.formOption.formData.rawYpBatch = JSON.parse(this.formOption.formData.rawYpBatch) || [{batch:''}]
            console.log(this.formOption.formData.rawYpBatch);
        }

    } else {
        this.modalOption.title = this.tabName+'-新增'
        // 根据不同的tab设置不同的默认数据结构
        if (this.tabName === '生产管理室') {
            this.formOption.formData = {
                registrationDate: '',
                varietyType: '',
                name: '',
                type: '',
                packDayNumReg: '',
                packDaySubNum: '',
                packDayNum: '',
                packDayShipNum: '',
                prodState: '',
                prodType: '',
                extractWorkshop: '',
                ycBatch: '',
                ycQualityDate: '',
                deliveryDate: ''
            }
        } else if (this.tabName === '饮片-提取-制剂车间') {
            this.formOption.formData = {
                name: this.editData?.name || '',
                ycTakeDate: '',
                tqwBatch: '',
                rawYpBatch: [{batch:''}],
                tqStartTime: '',
                gsEndTime: '',
                hySampleTime: '',
                sampleBatchSmall: '',
                sampleDate: '',
                pcDistributeDate: '',
                pcPlanDirection: '',
                tqReceivingPlanTime: '',
                crushingCompletionTime: '',
                zjReceivingPlanTime: '',
                zlFinishTime: '',
                klBatch: '',
                fsFlag: '',
                notZlReason: '',
                sampleDate2: '',
                planPackTime: '',
                packFinishTime: '',
                prodBatch: '',
                sampleBatchN: '',
                notPackReason: ''
            }
        } else if (this.tabName === '技术部') {
            this.formOption.formData = {
                inspectionBatchNoSample: '',
                tqwSampleTime: '',
                checkDepart: '',
                tqwCheckResultTime: '',
                zjPlanDistributeTime: '',
                planDirection: '',
                recordFlag: ''
            }
        } else if (this.tabName === '物控部') {
            this.formOption.formData = {
                granuleBatchNo: this.editData?.granuleBatchNo || '',
                finishedBatchNo: this.editData?.finishedBatchNo || '',
                klFsTime: '',
                klFsBackTime: '',
                prodFsTime: '',
                prodFsNoticeTime: '',
                prodTurnTime: ''
            }
        } else if (this.tabName === '质管部QA') {
            this.formOption.formData = {
                granuleBatchNo: this.editData?.granuleBatchNo || '',
                finishedBatchNo: this.editData?.finishedBatchNo || '',
                klReleaseTime: '',
                klNotReleaseReason: '',
                prodReleaseTime: '',
                prodNotReleaseReason: ''
            }
        } else {
            this.formOption.formData = {}
        }
    }
   }

  // 标准类型变化
standardChange(value: string) {
    this.getProductNameList(value)
  }
  // 获取品名列表
async getProductNameList(value: string) {
    let params = {
        type:'成品',
        standard: value,
        page: 1,
        spec:this.formOption.formData.type,
        pageSize: 10000
    }
    const res = await nextApi.getMaterialList(params)
    this.searchSource.productNameList = res.data.records || []
  }
addRawBatchItem(){
    this.formOption.formData.rawYpBatch.push({batch:''})
  }
deleteRawBatchItem(i:number){
    const list = this.formOption.formData.rawYpBatch || []
    if(list.length>1){
        list.splice(i,1)
    }
  }
handleDraft() {
    (this.$refs.formModel as any).validate(async (valid: boolean) =>{
        if(valid){
            try{
                this.loading = true
                let params:any = JSON.stringify(this.formOption.formData)
                params = JSON.parse(params)
                if(this.tabName==='饮片-提取-制剂车间'){

                    params.rawYpBatch = params.rawYpBatch.map((x:any)=> {
                        return {batch:x.batch}
                    })
                    params.rawYpBatch = JSON.stringify(params.rawYpBatch)
                    params.tableType = this.tabName
                    params.tempState = '1'
                    
                    const res = await nextApi.addOrUpdateProductionTableData(params)
                    this.$message.success(res.msg)
                    this.$emit('success')
                    this.handleCancel()
                    return
                }
                params.type = this.tabName
                params.status = '暂存'
                const res = await nextApi.addMaterialStatistics(params)
                this.$message.success(res.msg)
                this.$emit('success')
                this.handleCancel()
            }finally{
                this.loading = false
            }
        }
    })
}
// 提交表单
handleSubmit() {
    (this.$refs.formModel as any).validate(async (valid: boolean) =>{
        if(valid){
            try{
                this.loading = true
                let params:any = JSON.stringify(this.formOption.formData)
                params = JSON.parse(params)
                if(this.tabName==='生产管理室'){
                    params.tableType = this.tabName
                    params.tempState = '2'
                    const res = await nextApi.addOrUpdateProductionTableData(params)
                    this.$message.success(res.msg)
                    this.$emit('success')
                    this.handleCancel()
                    return
                }
                if(this.tabName==='技术部'){
                    params.tableType = this.tabName
                    params.tempState = '2'
                    const res = await nextApi.addOrUpdateProductionTableData(params)
                    this.$message.success(res.msg)
                    this.$emit('success')
                    this.handleCancel()
                    return
                }
                if(this.tabName==='饮片-提取-制剂车间'){
                    params.rawYpBatch = params.rawYpBatch.map((x:any)=> {
                        return {batch:x.batch}
                    })
                    params.rawYpBatch = JSON.stringify(params.rawYpBatch)
                    params.tableType = this.tabName
                    params.tempState = '2'
                    const res = await nextApi.addOrUpdateProductionTableData(params)
                    this.$message.success(res.msg)
                    this.$emit('success')
                    this.handleCancel()
                    return
                }
            if(this.tabName==='质管部QA'){
                    if(params.klReleaseTime && params.klNotReleaseReason){
                        this.$message.error('颗粒放行时间与未放行原因只能填写一个')
                        this.loading = false
                        return
                    }
                    if(params.prodReleaseTime && params.prodNotReleaseReason){
                        this.$message.error('成品放行时间与未放行原因只能填写一个')
                        this.loading = false
                        return
                    }
                    params.granuleStatus = this.computeStatus('klReleaseTime','klNotReleaseReason')
                    params.finishedStatus = this.computeStatus('prodReleaseTime','prodNotReleaseReason')
                }
                params.type = this.tabName
                const res = await nextApi.addOrUpdateProductionTableData(params)
                this.$message.success(res.msg)
                this.$emit('success')
                this.handleCancel()
            }finally{
                this.loading = false
            }
        }
    })
}

computeStatus(releaseKey: string, reasonKey: string) {
    const release = (this.formOption.formData as any)[releaseKey]
    const reason = (this.formOption.formData as any)[reasonKey]
    if (!release && !reason) return '待处理'
    if (release && !reason) return '已放行'
    if (!release && reason) return '未放行'
    return '待处理'
}

onGranuleReleaseChange() {
    if (this.formOption.formData.klReleaseTime && this.formOption.formData.klNotReleaseReason) {
    this.$message.error('颗粒放行时间与未放行原因只能填写一个')
    this.formOption.formData.klNotReleaseReason = ''
    }
}

onGranuleReasonChange() {
    if (this.formOption.formData.klReleaseTime && this.formOption.formData.klNotReleaseReason) {
    this.$message.error('颗粒放行时间与未放行原因只能填写一个')
    this.formOption.formData.klReleaseTime = ''
    }
}

onFinishedReleaseChange() {
    if (this.formOption.formData.prodReleaseTime && this.formOption.formData.prodNotReleaseReason) {
    this.$message.error('成品放行时间与未放行原因只能填写一个')
    this.formOption.formData.prodNotReleaseReason = ''
    }
}

onFinishedReasonChange() {
    if (this.formOption.formData.prodReleaseTime && this.formOption.formData.prodNotReleaseReason) {
    this.$message.error('成品放行时间与未放行原因只能填写一个')
    this.formOption.formData.prodReleaseTime = ''
    }
}
    


    // 取消
handleCancel() {
    this.$emit('update:visible', false)
    // 重置表单数据
    if (this.tabName === '生产管理室') {
        this.formOption.formData = {
            registrationDate: '',
            varietyType: '',
            name: '',
            type: '',
            packDayNumReg: '',
            packDaySubNum: '',
            packDayNum: '',
            packDayShipNum: '',
            prodState: '',
            prodType: '',
            extractWorkshop: '',
            ycBatch: '',
            ycQualityDate: '',
            deliveryDate: ''
        }
    } else if (this.tabName === '饮片-提取-制剂车间') {
        this.formOption.formData = {
            name: '',
            ycTakeDate: '',
            tqwBatch: '',
            rawYpBatch: '',
            rawBatchList: [{ batch: '' }],
            tqStartTime: '',
            gsEndTime: '',
            hySampleTime: '',
            sampleBatchSmall: '',
            sampleDate: '',
            pcDistributeDate: '',
            pcPlanDirection: '',
            tqReceivingPlanTime: '',
            crushingCompletionTime: '',
            zjReceivingPlanTime: '',
            zlFinishTime: '',
            klBatch: '',
            fsFlag: '',
            notZlReason: '',
            sampleDate2: '',
            planPackTime: '',
            packFinishTime: '',
            prodBatch: '',
            sampleBatchN: '',
            notPackReason: ''
        }
    } else if (this.tabName === '技术部') {
        this.formOption.formData = {
            inspectionBatchNoSample: '',
            tqwSampleTime: '',
            checkDepart: '',
            tqwCheckResultTime: '',
            zjPlanDistributeTime: '',
            planDirection: '',
            recordFlag: ''
        }
    } else if (this.tabName === '物控部') {
        this.formOption.formData = {
            granuleBatchNo: '',
            finishedBatchNo: '',
            klFsTime: '',
            klFsBackTime: '',
            prodFsTime: '',
            prodFsNoticeTime: '',
            prodTurnTime: ''
        }
    } else if (this.tabName === '质管部QA') {
        this.formOption.formData = {
            granuleBatchNo: '',
            finishedBatchNo: '',
            klReleaseTime: '',
            klNotReleaseReason: '',
            prodReleaseTime: '',
            prodNotReleaseReason: ''
        }
    }
}
}
</script>

<style lang="scss" scoped>
.container {
  .ant-card {
    .ant-card-head {
      background-color: #fafafa;
      border-bottom: 1px solid #d9d9d9;
    }
  }

  .ant-form-item {
    margin-bottom: 16px;
  }

  .ant-row {
    margin-bottom: 8px;
  }
}
</style>
